<template>
	<view>
		<Navbar :isReturn="false"></Navbar>
		<!-- <u-tabs-swiper ref="tabs" height="100" active-color="#B3723E" bg-color="transparent" :list="tabsData.list"
			:current="tabsData.current" :is-scroll="false" @change="onTabsChange"></u-tabs-swiper> -->
		<view v-if="[0].includes(tabsData.current)">
			<StickyTop :top="navbarHeight()">
				<view class="u-back-white u-m-20 u-p-t-20 u-p-b-20 u-radius-20 u-common-shadow">
					<view class="u-m-l-20 u-m-r-20">
						<u-search bg-color="#F3F4F6" height="80" placeholder="请输入项目关键词" v-model="keyword"
							:show-action="false" @search="onNewsSearch" @clear="onNewsSearch"></u-search>
					</view>
				</view>
			</StickyTop>
			<view class="u-m-20">
				<GiftList :list="giftList" @goGiftDetail="goGiftDetail"></GiftList>
			</view>
		</view>
		<view v-else-if="[1].includes(tabsData.current)">
			<view class="u-m-20">
				<DemandList :list="demandList" @goDemandDetail="goDemandDetail"></DemandList>
			</view>
		</view>
		<view v-else>
			<StickyTop :top="navbarHeight()">
				<view class="u-back-white u-m-20 u-p-t-20 u-p-b-20 u-radius-20 u-common-shadow">
					<view class="u-m-l-20 u-m-r-20">
						<u-search bg-color="#F3F4F6" height="80" placeholder="请输入资讯关键词" v-model="keyword"
							:show-action="false" @search="onNewsSearch" @clear="onNewsSearch"></u-search>
					</view>
					<u-tabs bg-color="#fff" active-color="#B3723E" :current="newsData.current" :list="newsData.list"
						@change="onNewsTypeChange"></u-tabs>
				</view>
			</StickyTop>
			<view class="u-m-20">
				<NewsList :list="newsList" @goNewsDetail="goNewsDetail"></NewsList>
			</view>
		</view>
		<Loadmore :status="status"></Loadmore>

		<SubmitBtn :text="submitText" @submit="onSubmit" v-if="[0].includes(tabsData.current) && ![0].includes(userStatus)"></SubmitBtn>
		<SubmitBtn :text="submitText" @submit="onSubmit" v-if="[1].includes(tabsData.current) && ![0].includes(userStatus) && [1].includes(roleStatus)"></SubmitBtn>

		<Tabbar></Tabbar>
	</view>
</template>

<script>
	import Navbar from '@/components/Navbar.vue'
	import Tabbar from '@/components/Tabbar.vue'
	import GiftList from '@/components/GiftList.vue'
	import NewsList from '@/components/NewsList.vue'
	import Loadmore from '@/components/Loadmore.vue'
	import StickyTop from '@/components/StickyTop.vue'
	import DemandList from '@/components/DemandList.vue'
	import SubmitBtn from '@/components/SubmitBtn.vue'
	import reachBottom from '@/mixins/reachBottom.js'
	import share from '@/mixins/share.js'
	import {
		newsType,
		newsList,
		newsDetail,
		userTogetherList,
		getShare,
		getConfigInfo
	} from '@/network/api.js'
	import {
		getDemandList
	} from '@/network/demand.js'
	export default {
		mixins: [share, reachBottom],
		components: {
			Navbar,
			Tabbar,
			GiftList,
			NewsList,
			Loadmore,
			DemandList,
			SubmitBtn,
			StickyTop
		},
		data() {
			return {
				keyword: '',
				newsData: {
					current: 0,
					typeId: 0,
					list: []
				},
				tabsData: {
					current: 2,
					list: [{
						name: '项目优选'
					}, {
						name: '求助'
					}, {
						name: '资讯'
					}],
				},
				submitText: '项目发布申请',
				giftList: [],
				newsList: [],
				demandList: [],
				roleStatus: 0, // 0 没权限  1 有权限
				userStatus: Number(uni.getStorageSync("user_status")), // 0 未注册  1 拉黑  2 已注册  3未激活
			}
		},
		onLoad(options) {
      wx.hideShareMenu()
			uni.showLoading({
				title: "加载中"
			})
			this.onReserveData()
			this.onGetNewsType()
			if (options.tabCurrent) {
				this.tabsData.current = Number(options.tabCurrent)
				switch (this.tabsData.current) {
					case 0:
						this.onGetTogetherList()
						break;
					case 1:
					    this.onGetDemandBtnRole()
						this.onGetDemandList()
						break;
					case 2:
						this.onGetNewsList()
						break;
				}
			} else {
				this.onGetNewsList()
			}
			this.onGetShare()
		},
		methods: {
			onTabsChange(val) {
				this.tabsData.current = val
				this.keyword = ''
				this.onGetShare()
				this.onReserveData()
				switch (val) {
					case 0:
						this.submitText = '项目发布申请'
						this.onGetTogetherList()
						break;
					case 1:
						this.submitText = '发布求助'
						this.onGetDemandBtnRole()
						this.onGetDemandList()
						break;
					case 2:
						this.onGetNewsList()
						break;
				}
			},
			onGetShare() {
				const params = {
					key: this.tabsData.current == 0 ? '49' : 'news'
				}
				getShare(params).then(res => {
					if (res.data) {
						res.data.page = `${res.data.page}?inviterId=${uni.getStorageSync("user_id")}`
						this.mixinsShare = res.data
            setTimeout(() => {
              wx.showShareMenu({
                withShareTicket: true,
                menus: ['shareAppMessage', 'shareTimeline']
              })
            }, 1000)
					}
				})
			},
			onGetTogetherList() {
				const params = {
					keyword: this.keyword,
					page: this.page,
					limit: this.pageSize
				}
				userTogetherList(params).then(res => {
					this.count = res.data.count
					res.data.list.map(item => this.giftList.push(item))
					if (this.giftList.length == this.count) {
						this.status = 'nomore'
					}
					uni.hideLoading()
				})
			},
			onGetDemandList() {
				const params = {
					keyword: this.keyword,
					page: this.page,
					limit: this.pageSize
				}
				getDemandList(params).then(res => {
					if (res.data.length > 0) {
						res.data.map(item => this.demandList.push(item))
					}
					if (res.data.length < 10) this.status = 'nomore'
					uni.hideLoading()
				})
			},
			async onGetDemandBtnRole(){
				const res = await getConfigInfo({key: 'deamnd_role'})
				const demandRole = res.data.value.split(',')
				const userRole = '' + uni.getStorageSync("president_status")
				if(demandRole.includes(userRole)){
					this.roleStatus = 1
				}else{
					this.roleStatus = 0
				}
			},
			onGetNewsType() {
				newsType().then(res => {
					res.data.unshift({
						id: '',
						name: '全部'
					})
					this.newsData.list = res.data
				})
			},
			onNewsTypeChange(val) {
				this.newsData.current = val
				this.newsData.typeId = this.newsData.list[val].id
				this.page = 1
				this.newsList.length = 0
				this.status = 'loadmore'
				this.onGetNewsList()
			},
			onGetNewsList() {
				const params = {
					keyword: this.keyword,
					type_id: this.newsData.typeId,
					page: this.page,
					limit: this.pageSize
				}
				newsList(params).then(res => {
					if (res.data.list.length > 0) {
						res.data.list.map(item => this.newsList.push(item))
					}
					if (res.data.list.length < 10) this.status = 'nomore'
					uni.hideLoading()
				})
			},
			onNewsSearch() {
				this.onReserveData()
				switch (this.tabsData.current) {
					case 0:
						this.onGetTogetherList()
						break;
					case 1:
						this.onGetDemandList()
						break;
					case 2:
						this.onGetNewsList()
						break;
				}
			},
			navbarHeight() {
				let topHeight = 0
				let height = uni.getDeviceInfo().system.includes('iOS') ? 44 : 48;
				topHeight = height + uni.getWindowInfo().statusBarHeight + 44 + 30
				return `${topHeight}rpx`
			},
			goGiftDetail(id) {
				uni.navigateTo({
					url: "/pages/giftDetail/giftDetail?id=" + id
				})
			},
			goNewsDetail(val) {
				switch(val.type){
					case 1:
					    uni.navigateTo({
					    	url: "/pages/newsDetail/newsDetail?id=" + val.id
					    })
						break;
					case 2:
					    uni.navigateTo({
					    	url: "/operate/webView/webView?url=" + val.weixin_url
					    })
						break;
					case 3:
					    wx.openChannelsActivity({
							finderUserName: val.video_auth_id,
							feedId: val.video_id,
							fail(err){
								console.log(err)
							}
						})
						break;
				}
			},
			goDemandDetail(val) {
				if(['0',0].includes(val.demand_status)){
					return
				}else{
					uni.navigateTo({
						url: "/operate/demandDetail/demandDetail?id=" + val.id
					})
				}
			},
			onSubmit() {
				switch (this.tabsData.current) {
					case 0:
						uni.navigateTo({
							url: "/operate/projectFeedBack/projectFeedBack"
						})
						break;
					case 1:
						uni.navigateTo({
							url: "/pages/resort/resort"
						})
						break;
				}
			},
			onReserveData() {
				this.page = 1
				this.status = 'loadmore'
				this.giftList = []
				this.newsList = []
				this.demandList = []
			},
			loadData() {
				if (this.status == 'nomore') {
					return
				}
				this.page++
				this.status = 'loading'
				switch (this.tabsData.current) {
					case 0:
						this.onGetTogetherList()
						break;
					case 1:
						this.onGetDemandList()
						break;
					case 2:
						this.onGetNewsList()
						break;
				}
			}
		}
	}
</script>

<style>
</style>